package com.jiankangjin.alipaychannel.services.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jiankangjin.alipaychannel.alipay.config.AlipayConfig;
import com.jiankangjin.alipaychannel.alipay.util.AlipayCore;
import com.jiankangjin.alipaychannel.services.PayService;
import com.jiankangjin.alipaychannel.util.XmlUtil;
import com.jiankangjin.alipaychannel.vo.PayXmlBean;
import com.jiankangjin.alipaychannel.vo.PayVo;
import com.jiankangjin.httpclient.HttpClient;

/**
 * 支付宝所有类型版本支付方法实现
 */
public class PayServiceImpl implements PayService {
	private static final transient Log logger = LogFactory.getLog(PayServiceImpl.class);

	/**
	 * 扫码付V0.0.1版
	 */
	@Override
	public PayVo barcode_V0_0_1(String pay_ssn_id, String order_id,
			String pay_fee, String pay_cry, String buyer_token,
			String buyer_voucher, String subjet, String remark,
			String buyer_name, String buyer_email, String seller_name,
			String seller_email, String expand_json) {
		PayVo payVo = new PayVo();
		//开始支付
		Map<String, String> sParaTemp = new HashMap<String, String>();
		sParaTemp.put("service", "alipay.acquire.createandpay");
        sParaTemp.put("partner", AlipayConfig.PARTNER);
        sParaTemp.put("_input_charset", "utf-8");
        sParaTemp.put("notify_url", AlipayConfig.NOTIFY_URL);
//		      sParaTemp.put("alipay_ca_request", null);
		sParaTemp.put("out_trade_no", pay_ssn_id);//商户唯一订单号
		sParaTemp.put("subject", "barcode_pay_test");//标题
		sParaTemp.put("product_code", "BARCODE_PAY_OFFLINE");//条码付
		sParaTemp.put("total_fee", "0.01");//金额
		sParaTemp.put("seller_id", AlipayConfig.PARTNER);//卖家支付宝用户号
		sParaTemp.put("seller_email", AlipayConfig.EMAIL);//卖家支付宝账号
//				sParaTemp.put("buyer_id", null);//买家支付宝用户号
//				sParaTemp.put("buyer_email", buyerEmail);//买家支会宝账号-手机号
//				sParaTemp.put("operator_type", "1");//商户操作员
//				sParaTemp.put("operator_id", "test-01");//卖家操作员id
//				sParaTemp.put("body", "1_fen_qian_test");//订单描述
//				sParaTemp.put("show_url", null);
//				sParaTemp.put("currency", "156");
//				sParaTemp.put("price", "0.01");//单价
//				sParaTemp.put("quantity", "1");//数量
		sParaTemp.put("goods_detail", "[{\"goodsName\":\"SHS_test_goods\","
									+ "\"price\":\"0.01\","
									+ "\"quantity\":\"1\"}]");//商品明细JSON
//				sParaTemp.put("extend_params", null);
//				sParaTemp.put("it_b_pay", itBpay);
//				sParaTemp.put("royalty_type", "ROYALTY");
//				sParaTemp.put("royalty_parameters", null);
//				sParaTemp.put("channel_parameters", null);
		sParaTemp.put("dynamic_id_type", "barcode");
		sParaTemp.put("dynamic_id", buyer_token);//买家支付宝动态条码

		//用支付宝的类和SDK进行RSA签名
		String privateKey = AlipayConfig.PRIVATE_KEY;
        Map<String, String> sPara = AlipayCore.buildSignPara(sParaTemp, privateKey);
        Map<String, Object> ssPara = new HashMap<String, Object>();
        for (String key : sPara.keySet()) {
			ssPara.put(key, sPara.get(key));
		}
        HttpClient httpClient = HttpClient.getInstance();
    	String service = "https://mapi.alipay.com/gateway.do?";
    	//提交到支付宝得到XML返回数据
    	if (logger.isInfoEnabled()) {
    		logger.info("调支付宝官方:"+ssPara);
    	}
    	String XMLstr = httpClient.doPOSTgetStr(service, ssPara);
    	//用dom4j的方式 把XML转为对象
    	PayXmlBean createAndPayVo = XmlUtil.parseCreateAndPayVo(XMLstr);    	
    	
    	payVo.setPay_ssn_id(createAndPayVo.getOutTradeNo());
    	payVo.setOrder_id(order_id);
    	payVo.setPay_cry(pay_cry);
    	payVo.setPay_fee(pay_fee);
    	payVo.setPay_chnl_seq(createAndPayVo.getTradeNo());
//		    	payVo.setPay_fin_time(pay_fin_time);
//		    	payVo.setPay_chnl_pos(pay_chnl_pos);
//		    	payVo.setPay_chnl_mer(pay_chnl_mer);
//		    	payVo.setPay_chnl_fee(pay_chnl_fee);
    	payVo.setPay_user(createAndPayVo.getBuyerLogonId());

    	String resultCode = createAndPayVo.getResultCode();
    	if (resultCode.equals("ORDER_SUCCESS_PAY_SUCCESS")) {
			//下单成功并且支付成功
    		payVo.setSub_deal_status("1001");
    		payVo.setSub_deal_msg("支付成功");
    		payVo.setPay_fin_time(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
			logger.info("订单"+order_id+"的支付"+pay_ssn_id+"下单成功并且支付成功");
		} 
		else if (resultCode.equals("ORDER_SUCCESS_PAY_INPROCESS")) {
			//下单成功支付处理中
			payVo.setSub_deal_status("0021");
    		payVo.setSub_deal_msg("支付处理中");
			logger.info("订单"+order_id+"的支付"+pay_ssn_id+"下单成功支付处理中 "+ createAndPayVo.getDetailErrorDes());
		} else {
			//ORDER_FAIL	UNKNOWN	ORDER_SUCCESS_PAY_FAIL	SOUNDWAVE_PARSER_FAIL 
			logger.info("订单"+order_id+"的支付"+pay_ssn_id+"支付失败 "+ createAndPayVo.getDetailErrorDes());
			payVo.setSub_deal_status("0041");
    		payVo.setSub_deal_msg("支付失败 "+createAndPayVo.getError() + " " +createAndPayVo.getDetailErrorDes());
		}
    	payVo.setResp_status("0000");
    	payVo.setResp_msg("成功");
    	payVo.setReturn_json("");
    	payVo.setExpand_json(expand_json);
    	payVo.setSign("Alipay_bracodeV0.0.1_sign");
		return payVo;
	}

	/**
	 * 公众号支付V0.0.1
	 */
	@Override
	public PayVo public_V0_0_1(String pay_ssn_id, String order_id,
			String pay_fee, String pay_cry, String buyer_token,
			String buyer_voucher, String subjet, String remark,
			String buyer_name, String buyer_email, String seller_name,
			String seller_email, String expand_json) {
		System.out.println("------------public_V0_0_1");
		return null;
	}

}
